package com.xinchuang.utils;

import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.ss.util.RegionUtil;

public class ExcelUtil {

    /**
     * 获取 sheet 页 rowNum 行
     *
     * @param sheet
     * @param rowNum
     * @return
     */
    public static Row getRow(Sheet sheet, int rowNum) {
        return sheet.getRow(rowNum) == null ? sheet.createRow(rowNum) : sheet.getRow(rowNum);
    }

    /**
     * 获取 row 行 colNum 列
     * @param row
     * @param colNum
     * @return
     */
    public static Cell getCell(Row row, int colNum) {
        return row.getCell(colNum) == null ? row.createCell(colNum) : row.getCell(colNum);
    }

    /**
     * 设置合并单元格边框
     * @param sheet
     * @param cellRangeAddress
     */
    public static void setCellRangeBorderThin(Sheet sheet, CellRangeAddress cellRangeAddress) {
        RegionUtil.setBorderTop(BorderStyle.THIN, cellRangeAddress, sheet);
        RegionUtil.setBorderBottom(BorderStyle.THIN, cellRangeAddress, sheet);
        RegionUtil.setBorderLeft(BorderStyle.THIN, cellRangeAddress, sheet);
        RegionUtil.setBorderRight(BorderStyle.THIN, cellRangeAddress, sheet);
    }
}
